# @Time    :2025/8/11 上午10:43
# @Author  :XiaoJiang
# @File    :orm单表查询.py
# @Software: PyCharm

import os

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Django_tushuguanli_demo.settings')
    import django

    django.setup()

    from app.models import Book, BookType, Press, Admin

    #01.查询所有
    # booklist = Book.objects.all()
    # print(booklist)

    #02.通过主键或其他字段查询
    # book = Book.objects.get(id=1)
    # print(book)

    #03.通过filter查询
    #得到的结果是集合。
    # booklist = Book.objects.filter(bookname__contains="王")
    # print(booklist)

    #04.通过values
    # booklist = Book.objects.values('bookname','bookauthor')
    # print(booklist)

    #05.exclude(参数） ：查询除了满足条件的所有数据
    # booklist = Book.objects.exclude(bookname__contains="王")
    # print(booklist)

    #06.order_by(参数)：排序
    # booklist = Book.objects.order_by('bookprice','-bookprice')
    # print(booklist)

    #07.reverse()：倒序
    # booklist = Book.objects.order_by('bookprice').reverse()
    # print(booklist)

    #08.count(): 统计数据条数
    # booklist = Book.objects.count()
    # print(booklist)

    #09.exists(): 判断数据是否存在
    # booklist = Book.objects.filter(bookname__contains="王").exists()
    # print(booklist)

    # 属性__gt >
    # 属性__gte >=
    # 属性__lt <
    # 属性__lte <=
    # id__gt=1,id__lt=5    select * from book where id>1 and id<5
    # 属性__range=[1,3]  select * from book where id between 1 and 3
    # 属性__contains="王"  select * from book where bookname like "%王%"       contains ：区分大小写的包含
    # 属性__startswith="王"  select * from book where bookname like "王%"
    # 属性__endswith="神"  select * from book where bookname like "%神"
    # 属性__incontains="王"  select * from book where bookname like "%王%"    icontains ：不区分大小写的包含

    # booklist = Book.objects.filter(bookname__endswith="神")
    # print(booklist)
000.